﻿#region Using Directives

using System;
using System.Web;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

#endregion Using Directives

namespace SharePointStu.ItemAuditing.Pages.Base
{
    public abstract class BaseAuditLog : LayoutsPageBase
    {
        #region Protected Properties

        protected Controls.AuditLogView AuditLogViewer;

        /// <summary>
        /// The default page title.
        /// </summary>
        protected const string DEFAULT_PAGE_TITLE = "Audit Log Information";

        #endregion Protected Properties

        #region Constants

        const string PROVIDER_NAME = "SPXmlContentMapProvider";

        #endregion Constants

        #region Protected Members

        protected Label lblWebName;
        protected Label lblWebURL;

        #endregion Protected Members

        #region Public Properties

        /// <summary>
        /// Get the identifier for the <see cref="T:Microsoft.SharePoint.SPWeb"/>  object to retrieve the audit information for.
        /// </summary>
        public Guid WebId
        {
            get { return SPContext.Current.Web.ID; }
        }

        #endregion Public Properties

        #region Overridden Methods

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            lblWebName.Text = SPContext.Current.Web.Name;
            lblWebURL.Text = SPContext.Current.Web.Url;
            SiteMapProvider siteMapProvider = SiteMap.Providers[PROVIDER_NAME];
            if (siteMapProvider != null)
                siteMapProvider.SiteMapResolve += CustomSiteMapResolve;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);

            // Detach from the static SiteMapResolve event and restore localization mode.
            SiteMapProvider siteMapProvider = SiteMap.Providers[PROVIDER_NAME];
            if (siteMapProvider != null)
                siteMapProvider.EnableLocalization = true;

            SiteMapProvider mapProvider = SiteMap.Providers[PROVIDER_NAME];
            if (mapProvider != null)
                mapProvider.SiteMapResolve -= CustomSiteMapResolve;
        }

        #endregion Overridden Methods

        #region Protected Methods

        protected abstract SiteMapNode CustomSiteMapResolve(object sender, SiteMapResolveEventArgs e);

        /// <summary> 
        /// Determines whether the two contexts are equal and, therefore, whether the SiteMap.SiteMapResolve event should be fired.
        /// </summary> 
        protected virtual bool IsSamePage(HttpContext context1, HttpContext context2)
        {
            // By default, the contexts are considered the same if they map to the same file and the same listid\itemid.
            bool match = (Server.MapPath(context1.Request.AppRelativeCurrentExecutionFilePath) == Server.MapPath(context2.Request.AppRelativeCurrentExecutionFilePath));
            return (match && (context1.Request.QueryString == context2.Request.QueryString));
        }

        #endregion Protected Methods
    }
}
